// Copyright (c) 2020 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

/**
*@describe:
*@author wfl19/Kristas
*@date 2021/12/18
 */

package select_mapper

import (
	"gitee.com/kristas/booting-go/framework/data/tk/example"
	"gorm.io/gorm"
)

type SelectExampleMapper struct {
	table string
	db    *gorm.DB
}

func NewSelectExampleMapper(table string, db *gorm.DB) *SelectExampleMapper {
	return &SelectExampleMapper{table: table, db: db}
}

func (s *SelectExampleMapper) SelectByExample(example example.Example, value Any) error {
	explain, values := example.Explain()
	return s.db.Table(s.table).Where(explain, values...).Find(value).Error
}
